package cn.huijielong.modules.oss.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;

import java.util.List;
import java.util.Map;

import cn.huijielong.modules.oss.cloud.OSSConfig;
import cn.huijielong.modules.oss.dao.SysOssDao;
import cn.huijielong.modules.oss.entity.Oss;
import cn.huijielong.modules.oss.entity.OssBucket;
import cn.huijielong.modules.oss.entity.SysOssEntity;
import cn.huijielong.modules.oss.service.SysOssService;



@Service("sysOssService")
public class SysOssServiceImpl implements SysOssService {
	@Autowired
	private SysOssDao sysOssDao;
	
	@Override
	public SysOssEntity queryObject(Long id){
		return sysOssDao.queryObject(id);
	}
	
	@Override
	public List<SysOssEntity> queryList(Map<String, Object> map){
		return sysOssDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return sysOssDao.queryTotal(map);
	}
	
	@Override
	public void save(SysOssEntity sysOss){
		sysOssDao.save(sysOss);
	}
	
	@Override
	public void update(SysOssEntity sysOss){
		sysOssDao.update(sysOss);
	}
	
	@Override
	public void delete(Long id){
		sysOssDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		sysOssDao.deleteBatch(ids);
	}
	@Override
	public OSSConfig getOSSConfig() {
		//从redis里取，没有值再从mysql取
		
		OSSConfig config=new OSSConfig();
		List<Oss> osses=sysOssDao.getOsses();
		List<OssBucket> buckets=sysOssDao.getOssBuckets();
		for(Oss oss:osses) {
			Oss oss2=new Gson().fromJson(oss.getConfig(), Oss.class);
			oss2.setId(oss.getId());
			oss2.setName(oss.getName());
			for(OssBucket b:buckets) {
				if(oss.getId()==b.getOssId()) {
					oss2.addBucket(b);
				}
			}
			config.add(oss2);
		}
		return config;
	}
}
